package com.kmxd.ams.core.common.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

/**
 * 检索类型
 *
 * @author nickbi
 */
@Getter
@AllArgsConstructor
public enum SearchTypeEnum {

  /** 检索 */
  QUICK(1, "快速检索"),
  ADVANCED(2, "高级检索"),
  CARD(3, "卡片检索"),
  CATALOG(4, "目录检索"),
  SUBJECT(5, "专题检索"),
  ;

  private Integer code;
  private String name;

  public static SearchTypeEnum convert(Integer type) {
    return Arrays.stream(values())
        .filter(x -> x.code.equals(type))
        .findAny()
        .orElseThrow(() -> BizException.instance(ErrorCode.NOT_FOUND, "未找到档案状态：" + type));
  }

  public static SearchTypeEnum convert(String name) {
    return Arrays.stream(values()).filter(x -> x.name.equals(name)).findAny().orElse(null);
  }

  @JsonCreator
  public static SearchTypeEnum getValue(Integer type) {
    return Arrays.stream(values()).filter(x -> Objects.equals(x.code, type)).findAny().orElse(null);
  }
}
